//注意：导航 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function () {
    var element = layui.element;

    //…
});

$(function () {

    //查看仓库信息

    warehousedetail = function (data) {
        layui.use('layer', function () {
            var layer = layui.layer;

            layer.open({

                title: ['仓库信息'],
                type: 2,
                content: '../../pages/warehouse/warehousedetail.html',
                shadeClose: true,
                area: ['1000px', '500px'],
                success: function (layero,index) {
                    console.log(data);
                    var body = layui.layer.getChildFrame('body', index);
                    body.find(".warehouseId").text(data.warehouseId);
                    body.find(".warehouseName").text(data.warehouseName);
                    body.find(".warehousePhone").text(data.warehousePhone);
                    body.find(".warehouseAddress").text(data.warehouseAddress);
                    layui.form.render();
                },
                cancel: function () {

                },
                end: function () {


                }
            });

        });
    }


});


layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function () {
    var laydate = layui.laydate //日期
        , laypage = layui.laypage //分页
        , layer = layui.layer //弹层
        , table = layui.table //表格
        , carousel = layui.carousel //轮播
        , upload = layui.upload //上传
        , element = layui.element //元素操作
        , slider = layui.slider //滑块

    //执行一个 table 实例
    table.render({
        elem: '#demo'
        , height: 600
        , url: 'http://localhost:8080/warehouse/search' //数据接口
        , title: '仓库表'
        , page: true //开启分页
        , title: '仓库信息表'
        , toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        ,cols: [ [ //表头
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'warehouseId', title: '仓库编号', width:'20%', sort: true}
            ,{field: 'warehouseName', title: '仓库名', width:'20%',sort: true}
            ,{field: 'warehousePhone', title: '仓库联系电话', width:'20%', sort: true}
            ,{field: 'warehouseAddress', title: '仓库地址', width:'20%', sort: true}
            ,{fixed: 'right', width: '16%', align:'center', toolbar: '#barDemo'}
        ] ]
        , parseData: function (res) { //res 即为原始返回的数据
            console.log(res)
            return {
                "code": res.code, //解析接口状态
                "data": res.data.list, //解析数据列表
                "count":res.data.total
            };
        }
    });

    //监听头工具栏事件
    table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'add':
                var url = '../../pages/warehouse/addwarehouse.html'
                $(window).attr('location', url);
                break;
            case 'update':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    var warehousedata = data[0];
                    localStorage.setItem('warehouse', JSON.stringify(warehousedata));
                    var url = '../../pages/warehouse/editwarehouse.html'
                    $(window).attr('location', url);

                }
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    layer.confirm("是否确认删除？", function () {
                        for (var i = 0; i < data.length; i++) {
                            var warehouseId = data[i].warehouseId;

                            $.ajax({
                                    type: "DELETE"
                                    , url: 'http://localhost:8080/warehouse/' + warehouseId
                                    ,success(){
                                        layer.msg("删除成功", {icon: '1', time: 2000})
                                        obj.del(); //删除对应行（tr）的DOM结构
                                    },error(){
                                        layer.msg("删除失败", {icon: '1', time: 2000})
                                    }
                                }
                            );
                            // window.location.reload();
                        }
                    });


                }
                break;
            case 'search':
                layui.use('layer', function () {
                    var layer = layui.layer;

                    layer.open({
                        title: ['查询仓库'],
                        type: 2,
                        content: '../../pages/warehouse/searchwarehouse.html',
                        shadeClose: true,
                        area: ['1000px', '600px'],
                    })
                })
                break;
            case 'refresh':
                window.location.reload();
                break;
        }
        ;
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
        console.log(data)
            , layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'detail') {
            warehousedetail(data);

        } else if (layEvent === 'del') {
            layer.confirm('是否确认删除？', function (index) {
                // obj.del(); //删除对应行（tr）的DOM结构
                layer.close(index);
                var warehouseId = obj.data.warehouseId;

                $.ajax({
                        type: "DELETE"
                        , url: 'http://localhost:8080/warehouse/' + warehouseId
                        ,success(){
                            layer.msg("删除成功", {icon: '1', time: 2000})
                            obj.del(); //删除对应行（tr）的DOM结构
                        },error(){
                            layer.msg("删除失败", {icon: '1', time: 2000})
                        }
                    }
                );

                // window.location.reload();

            });
        } else if (layEvent === 'edit') {
            var warehouseId = obj.data.warehouseId;
            localStorage.setItem('warehouse', JSON.stringify(data));
            var url = '../../pages/warehouse/editwarehouse.html'
            $(window).attr('location', url);
        }
    });
});
